package utils;


import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.UUID;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import log.LogManager;

/**
 * AES 加密工具类
 * 类名称：AESEncryptUtils
 * 类描述:
 * 创建人：heguang
 * 创建时间：2015年6月16日 下午8:29:01
 * 修改人：heguang
 * 修改时间：2015年6月16日 下午8:29:01
 * 修改备注:
 *
 * @see
 */
public class AESEncryptUtils {


    private static final String ALGORITHM = "AES";

    static final String CIPHER_ALGORITHM_ECB = "AES/ECB/PKCS5Padding";

    private static String CHARSETNAME = "UTF-8";


    /**
     * 生成随机密钥
     *
     * @return
     * @throws Exception
     */
    public static String getSecretKey() {
        return getSecretKey(null);
    }

    /**
     * 生成密钥
     *
     * @param seed 密钥种子
     * @return
     * @throws Exception
     */
    public static String getSecretKey(String seed) {

        if (seed == null || "".equals(seed.trim())) {
            seed = UUID.randomUUID().toString();
        }
        //将种子进行加密
        //        String hexString = DigestUtils.sha1Hex(seed);
        String hexString = new String(Hex.encodeHex(DigestUtils.sha1(seed)));
        //截取种子的16位
        String secretKey = hexString.substring(5, 21);
        try {
            //反正base64加密都的字符串
            return Base64Utils.encode(secretKey);
        } catch (Exception e) {
            LogManager.getLogger().e("operation failed. ", e.toString());
        }
        return null;
    }

    /**
     * 转换密钥
     *
     * @param key
     * @return
     * @throws Exception
     */
    private static SecretKeySpec toKey(String key) throws Exception {
        return new SecretKeySpec(Base64Utils.decode2byte(key), ALGORITHM);
    }

    /**
     * 加密
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static String encrypt(String source, String key) {

        try {
            SecretKeySpec secretKeySpec = toKey(key);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            byte[] data = source.getBytes(CHARSETNAME);
            data = cipher.doFinal(data);
            return Base64Utils.encode(data);
        } catch (Exception e) {
            LogManager.getLogger().e("operation failed. ", e.toString());
        }

        return null;
    }

    /**
     * 解密
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static String decrypt(String source, String key) {
        try {
            SecretKeySpec secretKeySpec = toKey(key);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte[] data = Base64Utils.decode2byte(source);
            data = cipher.doFinal(data);
            return new String(data);
        } catch (Exception e) {
            //throw new RuntimeException(e);
            e.printStackTrace();
            return null;
        }

    }

    /**
     * md5String md5
     *
     * @return Object 返回对象描述
     * @Exception 异常描述
     */
    public static String md5String(String sour) {
        return new String(Hex.encodeHex(DigestUtils.md5(sour))).toLowerCase();
    }
}
